/*
 * Copyright (c) 2018. czmc All Rights Reserved.
 * Author: czmc
 * Email: zxc6355533@163.com
 * Encoding: UTF-8
 */

package top.stime21.myneighbours.api

import android.content.Context
import org.greenrobot.eventbus.EventBus
import top.stime21.myneighbours.Events
import top.stime21.myneighbours.data.entity.User
import top.stime21.myneighbours.utils.ACache

/**
 * Created by czmc on 2016/8/26.
 */

class LocalDataManager private constructor() {
    fun getUser(context: Context): User? {
        if (user != null) return user
        var u = ACache.get(context).getAsObject("user")
        if (u != null) {
            user = u as User?
            return user
        }
        return null
    }

    fun setUser(context: Context, u: User) {
        ACache.get(context).put("user", u)
        user = u
    }

    fun logout(context: Context) {
        val aCache = ACache.get(context)
        aCache.remove("user")
        user = null
        EventBus.getDefault().post(Events.Logout())
    }

    companion object {
        private var ins: LocalDataManager? = null
        private val token = ""
        private var user: User? = null

        fun ins(): LocalDataManager {
            if (ins == null) {
                synchronized(LocalDataManager::class.java) {
                    if (ins == null) {
                        ins = LocalDataManager()
                    }
                }
            }
            return ins!!
        }
    }
}
